<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mod_cart extends CI_Controller {
    private $rear = '';
    private $content='Bạn đang cố gắng truy cập vào giỏ hàng!';
    function  __construct(){
        parent::__construct();
        if($this->session->userdata('ngonngu') && $this->session->userdata('ngonngu') == 'en'){
            $this->rear = '_en';
            $this->lang->load('atta','english');
        }else{
            $this->lang->load('atta','vietnam');
        }
        $this->load->library(array('session','form_validation','security',)); 
        $this->load->helper(array('form', 'text'));
        $this->load->model('home_model');
        
    }    
       
    function cart(){        
        $router=$this->security->xss_clean($this->uri->segment(2,0));
        $id=$this->security->xss_clean($this->uri->segment(3,0));
        switch($router){
            case 'add':
                if($_POST){
                    $qty=$_POST['qty'];
                }else $qty=1;  
                $this->addtocart($id,$qty);                                         
                break;
            case 'destroy':
                $this->destroy();
                break;
            case 'remov':
                $this->delete_cart($id);
                break;
            case 'update':
                if(!empty($_POST)){
                    $data= $this->security->xss_clean($_POST);
                    foreach($this->cart->contents() as $item){
                        $qty = $data[$item['id']];
                        $data1 = array(
                               'id'      => $item['id'],
                               'qty'     =>  $qty,
                               'price'   => $item['price'],
                               'name'    => $item['name']              
                            );
                       $this->updatecart($data1);
                    }
                }
                
                break;
        }
        $this->content=$this->load->view('mod_cart/cart','',true);
        $this->hienthi();
    }
    function test(){
        $this->content=$this->load->view('mod_cart/cart','',true);
        $this->hienthi();
    }
    function hienthi(){
        $data['rear'] = $this->rear;
        //$this->load->view('mod_cart/form_cart');
        $data['content'] = $this->content;
        $data['title'] = 'tiêu đề';
        $data['metades'] = 'metadesc';
        $data['metakey'] = 'metakey';
        $this->load->view('default/index',$data); 
    }
    function updatecart($data){
        $this->cart->product_name_rules = '\.\:\-_ a-z0-9áàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệóòỏõọơớờởỡợôốồổỗộúùủũụưứừửữựíìỉĩịýỳỷỹỵđÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÓÒỎÕỌƠỚỜỞỠỢÔỐỒỔỖỘÚÙỦŨỤƯỨỪỬỮỰÍÌỈĨỊÝỲỶỸỴĐ()';   
        $this->cart->insert($data);
    }    
    function addtocart($id='',$qty=1){
        $this->cart->product_name_rules = '\.\:\-_ a-z0-9áàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệóòỏõọơớờởỡợôốồổỗộúùủũụưứừửữựíìỉĩịýỳỷỹỵđÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÓÒỎÕỌƠỚỜỞỠỢÔỐỒỔỖỘÚÙỦŨỤƯỨỪỬỮỰÍÌỈĨỊÝỲỶỸỴĐ)(';   
        $data1 = $this->home_model->get_one('product',array('id'=>$id));
        if($this->cart->total_items()){
            foreach ($this->cart->contents() as $item){
                if($item['id']==$id){
                     $qty =$item['qty']+$qty;                    
                }
            }
        }
        $data = array(
               'id'      => $data1->id,
               'qty'     =>  $qty,
               'price'   => $data1->price,
               'name'    => $data1->title              
            );           
        $this->cart->insert($data); 
       
    }
    function delete_cart($id=''){        
        if($this->cart->total_items()){
           foreach ($this->cart->contents() as $item){
                if($item['id'] == $id){
                    $data['rowid']=$item['rowid'];
                    $data['qty'] = 0;
                    $this->cart->update($data); 
                    break; 
                }
           }
        }        
    }
    function destroy(){
        $this->cart->destroy();
        redirect(base_url());
    }
    function checkout(){    
        //$this->session->unset_userdata('logedin');
        if($this->session->userdata('logedin')){
            $this->db->insert('order',array('id_user'=>$this->session->userdata('id')));
            $id_order= $this->home_model->select_max('order','id');
            if($this->cart->contents()){
                $ip=$this->input->ip_address();
                foreach ($this->cart->contents() as $item){
                    $id_sp = $item['id'];
                    $price= $item['price'];
                    $qty = $item['qty'];
                    $data= array(
                        'id_order'=>$id_order,
                        'ip'=>$ip,
                        'qty'=>$qty,
                        'id_sp'=>$id_sp,
                        'price'=>$price
                        );
                  $this->db->insert('order_details',$data);  
                }
                $this->content = '<span style="color:red;padding:20px;margin-top:30px;">Gửi đơn hàng thành công, chúng tôi sẽ kiểm tra và liên hệ lại với bạn trong thời gian sớm nhất</span>';
                 $this->cart->destroy();
            }
            
        }else{
             $this->content= 'Bạn cần phải đăng nhập để thực hiện thanh toán,<br/>
            <a href="'.base_url().'users/login">Đăng nhập</a><br/>
            <a href="'.base_url().'users">Đăng ký tài khoản</a>
            ';
        }
        $this->hienthi();
        
               
    }
}